博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
分页器(自定制)
阅读量:5903 次
发布时间:2019-06-19

本文共 7059 字,大约阅读时间需要 23 分钟。

一、函数版  (未更新)

1 def show_books(request): 2     """ 3     自定制分页器(函数版): 4     :param request:  5     :return:    data_list: 希望展示在当前页面的数据集合 6                 page_html: 页码导航条html 7     """ 8     url = request.path_info 9 10     all_book_list = models.Book.objects.all()11     total_data = all_book_list12 13     try:14         total_data_num = total_data.count()  # 总数据条数15     except TypeError:16         total_data_num = len(total_data)17 18     current_page_id = request.GET.get('page')  # 当前页码id19     try:20         current_page_id = int(current_page_id)21     except (ValueError, TypeError):22         current_page_id = 123 24     perpage_data_num = 10  # 每个页面展示数据条数25 26     quotient, more = divmod(total_data_num, perpage_data_num)27     total_page_num = quotient+1 if more != 0 or quotient == 0 and more == 0 else quotient  # 总页码数28 29     # 控制当前页码乱入负数、太大数等情况30     if current_page_id < 1:31         current_page_id = 132     elif current_page_id > total_page_num:33         current_page_id = total_page_num34 35     pagedata_start_id = (current_page_id - 1) * perpage_data_num36     pagedata_end_id = current_page_id * perpage_data_num37 38     nvgtnbar_page_num = 9  # 页面中页码导航条显示的页码个数39     left_page_id = current_page_id - nvgtnbar_page_num // 2  # 页码导航条最左边的页码id40     right_page_id = current_page_id + nvgtnbar_page_num // 2  # 页码导航条最右边的页码id41 42     # 两端控制溢出43     if left_page_id < 1:44         left_page_id = 145         right_page_id = nvgtnbar_page_num46     if right_page_id > total_page_num:47         right_page_id = total_page_num48         left_page_id = total_page_num - nvgtnbar_page_num + 149 50     # 数据量不足的情况下,控制页码导航条的显示51     if total_page_num < nvgtnbar_page_num:52         left_page_id = 153         right_page_id = total_page_num54 55     page_html = ''56     prev_page_id = current_page_id - 1 if current_page_id != 1 else current_page_id57     next_page_id = current_page_id + 1 if current_page_id != total_page_num else current_page_id58     first_page_html = '
  • 首页
  • '.format(url)59 last_page_html = '
  • 尾页
  • '.format(url, total_page_num)60 prev_page_html = '
  • 上一页
  • '.format(url, prev_page_id)61 next_page_html = '
  • 下一页
  • '.format(url, next_page_id)62 63 for i in range(left_page_id, right_page_id + 1):64 if i == current_page_id:65 page_html += '
  • {1}
  • '.format(url, i)66 else:67 page_html += '
  • {1}
  • '.format(url, i)68 69 page_html = prev_page_html + first_page_html + page_html + last_page_html + next_page_html70 71 data_list = total_data[pagedata_start_id:pagedata_end_id]72 73 return render(request, 'book_list.html', {
    'data_list': data_list, 'page_html': page_html})
    分页器示例函数(未封装)

    二、分页器(封装版)  (已更新)

    class Pagination(object):    def __init__(self, request, data_set, perpage_data_count=10, nvgbar_page_count=9):        """        自定制分页器:                        供调用属性                        设定接收分页器处理后结果的对象为mypage                        mypage.data:     希望展示在当前页面上的数据集合                        mypage.page_html:页码导航栏html        :param request:     request请求        :param data_set:        希望进行分页处理的容器性数据对象,即总数据        :param perpage_data_count:    希望每个页面要展示的数据条数,默认10条        :param nvgbar_page_count:    希望页码导航条展示的页码数,默认9个页码        """        self.url = request.path_info        self.perpage_data_count = perpage_data_count        self.nvgbar_page_count = nvgbar_page_count        current_page_id = request.GET.get('page')        try:            current_page_id = int(current_page_id)        except (ValueError, TypeError):            current_page_id = 1        self.current_page_id = current_page_id        try:            total_data_count = data_set.count()        except TypeError:            total_data_count = len(data_set)        # quotient, more = divmod(total_data, self.perpage_data)        # self.total_page_count = quotient + 1 if more != 0 or quotient == 0 and more == 0 else quotient  # 总页码数        self.total_page_count, more = divmod(total_data_count, self.perpage_data_count)        if more:            self.total_page_count += 1        # 控制当前页码乱入负数、太大数等情况(两个if,顺序不能反)        if self.current_page_id > self.total_page_count:            self.current_page_id = self.total_page_count        if self.current_page_id < 1:            self.current_page_id = 1        perpagedata_start = (self.current_page_id - 1) * self.perpage_data_count        perpagedata_end = self.current_page_id * self.perpage_data_count        self.data = data_set[perpagedata_start: perpagedata_end]    @property    def page_html(self):        left_page_id = self.current_page_id - self.nvgbar_page_count // 2  # 页码导航条最左边的页码id        right_page_id = self.current_page_id + self.nvgbar_page_count // 2  # 页码导航条最右边的页码id        # 两端控制溢出        if left_page_id < 1:            left_page_id = 1            right_page_id = self.nvgbar_page_count        if right_page_id > self.total_page_count:            right_page_id = self.total_page_count            left_page_id = self.total_page_count - self.nvgbar_page_count + 1        # 数据量不足的情况下,控制页码导航条的显示        if self.total_page_count < self.nvgbar_page_count:            left_page_id = 1            right_page_id = self.total_page_count        page_html_list = ['
    ') page_html = ''.join(page_html_list) return page_html
    分页器(封装版)
    视图中使用分页器:
    from utils.mypage import Pagination    def show_books(request):    book_list = models.Books.objects.all()    # book_list = models.Books.objects.all().value_list('title', 'authors')    show_obj = Pagination(request, book_list)    return render(request, 'books.html', {
    'show_obj': show_obj})

    模板中:

        
    Title
    {
    % for book in show_obj.data %}
    {
    % endfor %}
    序号 ID 书名
    {
    { forloop.counter }}
    {
    { book.id }}
    {
    { book.title }}
    {
    { show_obj.page_html|safe }}
    模板html中调用

     

     

     

     

    ok

     

    转载于:https://www.cnblogs.com/kingon/p/9452240.html

    你可能感兴趣的文章
    php - 字符串处理
    查看>>
    bulk collect 以及ref cursor使用
    查看>>
    mysql性能优化-慢查询分析、优化索引和配置
    查看>>
    图解分布式一致性协议Paxos
    查看>>
    Jedis与Redisson选型对比
    查看>>
    MongoDB学习笔记(查询)
    查看>>
    freemarker自定义标签的写法和使用
    查看>>
    使用Gitlab CI进行持续集成
    查看>>
    Win32编程基本概念
    查看>>
    那些年我所留恋的
    查看>>
    ×××灯式样的站点链接说明,链接提示
    查看>>
    Linux下动态IP和静态IP的设置方法
    查看>>
    mysql 行长度
    查看>>
    SUSE配置网关
    查看>>
    java中获取字母和数字的组合
    查看>>
    8-3 泛型
    查看>>
    你是“职业”软件开发吗?——书评《浮现式设计-专业软件开发的演进本质》...
    查看>>
    iOS 多线程 之 GCD(大中枢派发)(二)
    查看>>
    开源项目 log4android 使用方式详解
    查看>>
    ssh命令详解
    查看>>